package com.g2room.search;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;



public class DbUtils {
	public static void main(String[] args) {
	}
	/**
	 * 获取数据库连接对象，这里只获得，需要自己在使用后进行关闭。
	 * 此处单线程执行，要并发则使用连接池
	 *
	 * @return {Connection}
	 * @auther <a href="mailto:gaoxg@FOUNDER.COM.CN">Gao Xuguo</a>
	 * 2013-2-25 下午04:36:46
	 */
	public static Connection getConn(String url, String userName, String pwd) {
		Connection conn = null;
		try {
		    //1.加载驱动
		    Class.forName("com.mysql.jdbc.Driver");
		    //2.得到连接
		    conn = DriverManager.getConnection(
		    		"jdbc:mysql://" + url + "?useUnicode=true&characterEncoding=utf8&mysqlEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull",
		    		userName,
		    		pwd);
		} catch(Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * 关闭连接。
	 *
	 * @param {Connection} conn
	 * @auther <a href="mailto:gaoxg@FOUNDER.COM.CN">Gao Xuguo</a>
	 * 2013-2-25 下午04:39:25
	 */
	public static void closeConn(Connection conn) {
		if(null != conn) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	public static Connection getMyuallConn() {
		Connection connection = null;
		try {
			connection = DbUtils.getConn(
					"127.0.0.1:3306/myuall", 
//					"127.0.0.1:3306/myuall_ol", 
					"root", "123456");
//					"218.29.188.54:3306/myuall", 
//					"op", "!wlms*yts_asw");
//			"218.29.188.54:3306/myuall", 
//				connection = new DbUtil().getConn("com.mysql.jdbc.Driver", 
//						"jdbc:mysql://127.0.0.1:3306/myuall?useUnicode=true&characterEncoding=utf8&mysqlEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull", 
//						"root", "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return connection;
	}
	public static Connection getNewsConn() {
		Connection connection = null;
		try {
			connection = DbUtils.getConn(
					"127.0.0.1:3306/news", 
					"root", "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return connection;
	}
	public static Connection getCompanyConn() {
		Connection connection = null;
		try {
			connection = DbUtils.getConn(
					"127.0.0.1:3306/company", 
					"root", "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return connection;
	}
	public static Connection getPeeConn() {
		Connection connection = null;
		try {
			connection = DbUtils.getConn(
//					"127.0.0.1:3306/myuall", 
					"127.0.0.1:3306/kaoyan", 
					"root", "");
//					"218.29.188.54:3306/myuall", 
//					"op", "!wlms*yts_asw");
//			"218.29.188.54:3306/myuall", 
//				connection = new DbUtil().getConn("com.mysql.jdbc.Driver", 
//						"jdbc:mysql://127.0.0.1:3306/myuall?useUnicode=true&characterEncoding=utf8&mysqlEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull", 
//						"root", "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return connection;
	}

	/**
	 * 获取数据库连接对象，这里只获得，需要自己在使用后进行关闭。
	 *
	 * @return 数据库连接实例
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 * @auther <a href="mailto:g2room@163.com">天涯</a>
	 * Jul 6, 2013 8:49:25 PM
	 */
	public Connection getConn(String driverName, String url, String username, String pwd)
			throws ClassNotFoundException, SQLException {
		// 1.加载驱动
		Class.forName(driverName);
		// 2.得到连接
		return DriverManager.getConnection(url, username, pwd);
	}
	
	public static String charset(String str) {
		try {
			str = new String(str.getBytes( "ISO-8859-1"), "GBK");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}
}
